<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学期" prop="semId">
        <el-select v-model="queryParams.semId" filterable clearable placeholder="请选择学期">
          <el-option
            v-for="item in semsterList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" clearable filterable placeholder="请选择班级">
          <el-option
            v-for="item in classList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程" prop="courseId">
        <el-select v-model="queryParams.courseId" placeholder="请输入课程" clearable filterable>
          <el-option
            v-for="dict in courseList"
            :key="dict.id"
            :label="dict.title"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="教师工号" prop="code">
        <el-input v-model="queryParams.code" type="text" placeholder="请输入教师工号" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teachPlanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学期" align="center" prop="semName" />
      <el-table-column label="课程" align="center" prop="courseName" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="教师工号" align="center" prop="code" />
      <el-table-column label="教师姓名" align="center" prop="name" />
      <el-table-column label="学校代码" align="center" prop="schoolCode" />
      <el-table-column label="教学计划照片" align="center" prop="file">
        <template slot-scope="scope">
          <el-image
            style="width: 120px;height: 60px;"
            :src="scope.row.file"
            :preview-src-list="[scope.row.file]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教学计划对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="学期" prop="semName">
            <el-select v-model="form.semName" filterable clearable placeholder="请选择学期" @change="changesem">
              <el-option
                v-for="item in semsterList"
                :key="item.id"
                :label="item.title"
                :value="[item.id,item.title]"
                size="small">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="班级" prop="classId">
            <el-select v-model="form.classId" clearable placeholder="请选择班级" filterable>
              <el-option
                v-for="item in classList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
                size="small">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="课程" prop="courseId">
            <el-select v-model="form.courseId" placeholder="请输入课程" clearable filterable>
              <el-option
                v-for="dict in courseList"
                :key="dict.id"
                :label="dict.title"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="教师" prop="teacherId">
            <el-select v-model="form.teacherId" placeholder="请输入课程" clearable filterable>
              <el-option
                v-for="dict in teacherList"
                :key="dict.userId"
                :label="dict.name"
                :value="dict.userId"
              />
            </el-select>
          </el-form-item>
        <el-form-item label="教师工号" prop="code">
          <el-input v-model="form.code" type="text" placeholder="请输入教师工号" />
        </el-form-item>
        <el-form-item label="学校代码" prop="schoolCode">
          <el-input v-model="form.schoolCode" type="text" placeholder="请输入学校代码" />
        </el-form-item>
          <el-form-item label="学生分析" prop="student">
            <el-input v-model="form.student" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="教学目标" prop="target">
            <el-input v-model="form.target" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="教学措施" prop="measure">
            <el-input v-model="form.measure" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="教学计划图片" prop="file">
            <imageUpload v-model="form.file" :limit="20" />
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getTeachPlanDetail,
  updateTeachPlangarden,
  exportTeachPlan,
  listTeachPlangarden, addTeachPlangarden, delTeachPlangarden, exportTeachPlangarden
} from '@/api/teach/teachPlan'
import { getToken } from "@/utils/auth";
import { selectSemster} from "@/api/base/semster";
import { selectClazz } from "@/api/base/clazz";
import { selectCourse} from "@/api/base/course";
import { selectTeacher} from "@/api/base/teacher";

export default {
  name: "TeachPlan",
  data() {
    return {
      //最大课时
      periods: 48,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教学计划表格数据
      teachPlanList: [],
      // 学期数据
      semsterList: [],
      // 班级表格数据
      classList: [],
      // 教师表格数据
      teacherList: [],
      // 课程数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        semId: [
          { required: true, message: "学期不能为空", trigger: "blur" }
        ],
        specId: [
          { required: true, message: "专业不能为空", trigger: "blur" }
        ],
        courseId: [
          { required: true, message: "课程不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "教师不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getSemster();
    this.getCourse();
    this.getTeacher();
    this.getClazz();
  },
  methods: {
    /**学期切换*/
    changesem(value){
      this.form.semId=value[0];
      this.form.semName=value[1]
    },
    /**班级*/
    async getClazz(){
      try {
        const {rows}=await selectClazz();
        this.classList = rows
      }catch (e) {
        console.log(e)
      }
    },
    /**教师**/
    async getTeacher(){
      try {
        const {rows}=await selectTeacher();
        this.teacherList = rows;
      }catch (e) {
        console.log(e)
      }
    },
    /**课程*/
    async getCourse(){
      try {
        const { rows }=await selectCourse();
        this.courseList = rows;
      }catch (e) {
        console.log(e)
      }
    },
    /**学期*/
    async getSemster(){
      try {
        const { rows }=await selectSemster();
        this.semsterList =rows;
      }catch (e) {
        console.log(e)
      }
    },
    /** 查询教学计划列表 */
    async getList() {
      try {
        this.loading = true;
        const data = await listTeachPlangarden(this.queryParams);
        this.teachPlanList = data.rows;
        this.total = data.total;
        this.loading = false;
      } catch (e) {
        console.log(e)
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        semId: null,
        semName: null,
        courseId: null,
        courseName:null,
        name:null,
        classId: null,
        teacherId: null,
        file: null,
        student: null,
        target: null,
        measure: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教学计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.form = row;
      this.form=Object.assign({}, this.form, row)
      this.open = true;
      this.title = "修改教学计划";
    },
    /** 修改按钮操作 */
    // handleExtend(row) {
    //   this.reset();
    //   const id = row.id || this.ids
    //   getTeachPlanDetail(id).then(response => {
    //     this.form = response.data;
    //
    //     var ps = [];
    //     for(var i=1;i<=this.periods;i++) {
    //       ps.push({id:null,period:i,content:null});
    //     }
    //
    //     for(var i=0;i<ps.length;i++) {
    //       for(var j=0;j<response.data.periods.length;j++){
    //         if(response.data.periods[j].period == ps[i].period){
    //           ps[i].content = response.data.periods[j].content;
    //         }
    //       }
    //     }
    //     this.form.periods = ps;
    //     this.form.id == null;
    //     this.form.semId = null;
    //     this.open = true;
    //     this.title = "修改教学计划";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeachPlangarden(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeachPlangarden(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除教学计划编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delTeachPlangarden(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教学计划数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportTeachPlangarden(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
<style lang="scss" scoped>
.el-dialog__body .el-select,.el-dialog__body .el-date-editor{
  width: 100%;
}
</style>
